﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace FinancialManagement.Models.Repository
{
    public class ComboBoxRepository
    {
        internal string GetRevenueTypeNameByCode(string code)
        {
            using (var dc = new FinancialMGTDataContext())
            {
                var query = from t in dc.DIC_RevenueType
                            where t.RevenueTypeCode.Equals(code)
                            select t.RevenueTypeName;
                if (query != null && query.Count() > 0)
                {
                    return query.FirstOrDefault();
                }
                else
                {
                    throw new NotFindFromDatabase(Utils.Util.ERROR);
                }
            }
        }

        internal string GetChargeScopeNameByCode(string code)
        {
            using (var dc = new FinancialMGTDataContext())
            {
                var query = from t in dc.DIC_ChargeScope
                            where t.ChargeScopeCode.Equals(code)
                            select t.ChargeScopeName;
                if (query != null && query.Count() > 0)
                {
                    return query.FirstOrDefault();
                }
                else
                {
                    throw new NotFindFromDatabase(Utils.Util.ERROR);
                }
            }
        }

        internal string GetCalculateUniteNameByCode(string code)
        {
            using (var dc = new FinancialMGTDataContext())
            {
                var query = from t in dc.DIC_CalculateUnit
                            where t.CalculateUnitCode.Equals(code)
                            select t.CalculateUnitName;
                if (query != null && query.Count() > 0)
                {
                    return query.FirstOrDefault();
                }
                else
                {
                    throw new NotFindFromDatabase(Utils.Util.ERROR);
                }
            }
        }

        internal IList<DIC_RevenueType> GetRevenueTypes()
        {
            using (var dc = new FinancialMGTDataContext())
            {
                var query = dc.DIC_RevenueType.Where(o => o.RevenueTypeCode != null).ToList();
                return query;
            }
        }

        internal IList<DIC_ChargeScope> GetChargeScopes()
        {
            using (var dc = new FinancialMGTDataContext())
            {
                var query = dc.DIC_ChargeScope.Where(o => o.ChargeScopeCode != null).ToList();
                return query;
            }
        }

        internal IList<DIC_CalculateUnit> GetCalculateUnits()
        {
            using (var dc = new FinancialMGTDataContext())
            {
                var query = dc.DIC_CalculateUnit.Where(o => o.CalculateUnitCode != null).ToList();
                return query;
            }
        }

        internal IList<Department> GetDepartments()
        {
            using (var dc = new FinancialMGTDataContext())
            {
                var query = (from d in dc.T_Department
                             where d.DepartmentID != null
                             select new Department
                             {
                                 BudgetAuthority = d.BudgetAuthority,
                                 BudgetAuthorityID = d.BudgetAuthorityID,
                                 DepartmentID = d.DepartmentID,
                                 DepartmentName = d.DepartmentName,
                                 DepChiefID = d.DepChiefID,
                                 DepChiefName = d.DepChiefName,
                                 Notes = d.Notes,
                                 PhoneNumber = d.PhoneNumber
                             }).ToList();
                return query;
            }
        }

        internal IList<ProjectTable> GetProjectTables()
        {
            using (var dc = new FinancialMGTDataContext())
            {
                var query = (from d in dc.T_ProjectTable
                             where d.ProjectID != null
                             select new ProjectTable
                             {
                                 ProjectID = d.ProjectID,
                                 ProjectName = d.ProjectName,
                                 Notes = d.Notes,
                             }).ToList();
                return query;
            }
        }
    }
}